<script lang="ts" setup name="XtxCity">
import { ref, watch } from 'vue';
import { onClickOutside } from '@vueuse/core'

import { AreaList, CityResult } from './city'
import axios from 'axios'

// 子组件接收父组件传过来的城市数据
defineProps<{
  userAddress?: string
}>()


// active: 控制显示框 - 布尔
const active = ref(false)

// 切换显示框
const toggle = () => {
  active.value = !active.value
}

// 点击元素的外面 , 关闭弹框
const target = ref(null)
onClickOutside(target, () => {
  active.value = false
})


// 定义城市列表
const cityList = ref<AreaList[]>([])
// 备份 - 城市列表
const cacheList = ref<AreaList[]>([])

// axios 发送请求, 获取城市数据...
const getCityList = async () => {
  const res = await axios.get<AreaList[]>('https://yjy-oss-files.oss-cn-zhangjiakou.aliyuncs.com/tuxian/area.json')
  // console.log('---城市列表---', res.data);
  cityList.value  = res.data
  cacheList.value = res.data
}
getCityList()

// 收集选择城市信息
const changeResult = ref({
  provinceCode: '',
  provinceName: '',
  cityCode: '',
  cityName: '',
  countyCode: '',
  countyName: ''
})

const emit = defineEmits<{
  (e: 'changeCity', value: CityResult): void
}>()


// 城市切换
const selectCity = (city: AreaList) => {
  // city.level 
  // 0 - 省 
  if (city.level === 0) {
    cityList.value = city.areaList
    changeResult.value.provinceName = city.name
    changeResult.value.provinceCode = city.code
  } 
    // 1 - 市
  if (city.level === 1) {
    cityList.value = city.areaList
    changeResult.value.cityName = city.name
    changeResult.value.cityCode = city.code
  } 
    // 2 - 县/区 关闭弹框
  if (city.level === 2) {
    active.value = false
    changeResult.value.countyName = city.name
    changeResult.value.countyCode = city.code
    // console.log(changeResult.value);
    // 子传父
    emit('changeCity', changeResult.value)
  }
}

// watchEffect 是一进入页面就会执行一次, 而当前是不需要一进入页面就执行的....
// 关闭时, 恢复选择的城市数据
// 监听弹框是否关掉 - active变量
watch(active, (value) => {
  if (!value) {
    // 关闭了弹框 - 恢复数据
    cityList.value = cacheList.value // 原始的数据
  }
})
</script>
<template>
  <div class="xtx-city" ref="target">
    <!-- 城市选择点击控件 -->
    <div class="select" :class="{active: active }"  @click="toggle">
      <span class="value" v-if="userAddress">
        <!-- 父组件传过来的城市数据 -->
        {{ userAddress }}
      </span>
      <span class="placeholder" v-else>请选择配送地址</span>
      <i class="iconfont icon-angle-down"></i>
    </div>

    <!-- 显示框 - 省市区 -->
    <div class="option" v-show="active">
      <span class="ellipsis" @click="selectCity(item)" v-for="item in cityList" :key="item.code">
        {{ item.name }}
      </span>
    </div>
  </div>
</template>

<style scoped lang="less">
.xtx-city {
  display: inline-block;
  position: relative;
  z-index: 400;
  margin-left: 5px;
  .select {
    border: 1px solid #e4e4e4;
    height: 30px;
    padding: 0 5px;
    line-height: 28px;
    cursor: pointer;
    &.active {
      background: #fff;
    }
    .placeholder {
      color: #999;
    }
    .value {
      color: #666;
      font-size: 12px;
    }
    i {
      font-size: 12px;
      margin-left: 5px;
    }
  }
  .option {
    width: 542px;
    border: 1px solid #e4e4e4;
    position: absolute;
    left: 0;
    top: 29px;
    background: #fff;
    min-height: 30px;
    line-height: 30px;
    display: flex;
    flex-wrap: wrap;
    padding: 10px;
    > span {
      width: 130px;
      text-align: center;
      cursor: pointer;
      border-radius: 4px;
      padding: 0 3px;
      &:hover {
        background: #f5f5f5;
      }
    }
  }
}
</style>